<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>数组</title>
</head>

<body>

</body>

</html>
<script>
    // let arr = [];
    // Array.prototype.push = function () {
    //     for (var i = 0; i < arguments.length; i++) {
    //         this[this.length] = arguments.length;
    //     }
    //     return this.length
    // }


    var arr1 = [1, 2, 3, 9, 34, , 5, 36, 4, 67, 89, 12, 23, 33, 45];
    // arr.splice(从第几位开始,截取多少长度,在切口处添加新数据)

    splice = function (pos) {
        pos += pos > 0 ? 0 : this.lenght;
    }

    /* 
    sort 必须写两个形参
    看返回值 
    当返回值为负数时,那么前面的数放在前面
    为正数 那么后面的数在前
    为 0 不动 
    */
    arr1.sort(function (a, b) {
        //    return a - b;
        return b - a;
    });


    // 给一个数组乱序
    var arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    arr2.sort(() => {
        return Math.random() - 0.5;
    })

    // 按字节长度排序


    function retBytes(str) {
        var num = str.length;
        for (var i in str) {
            if (str.charCodeAt(str[i]) > 255) {
                num++
            }
        }
        // for(var i = 0; i < str.length; i++){
        //     if (str.charCodeAt(i) > 255) {
        //         console.log(num)
        //         num ++;
        //         console.log(i)
        //     }
        // }
        return num
    }

    var strArr = ['爽肤水发的萨芬撒地方', 'a等', 'bb等', '等', 'c']
    strArr.sort(function (a, b) {
        return retBytes(a) - retBytes(b);
    })
    console.log(strArr)

    //    类数组

    var obj = {
        "0": "a",
        "1": "b",
        "2": "c",
        "length": 3,
        "push": Array.prototype.push,
        "splice": Array.prototype.splice
    }

    var obj1 = {
        "2": "a",
        "3": "b",
        "length": 2,
        "push": Array.prototype.push,
        "splice": Array.prototype.splice
    }
    obj1.push('c');
    obj1.push('d');


    // 类型判断
    /* 
        分为原始值 和引用值
        区分引用值
        */
    function type(target) {
        var ret = typeof (target);
        var template = {
            "[object Array]": "array",
            "[object Object]": "object",
            "[object Nmuber]": "number - object",
            "[object Boolean]": "boolean-object",
            "[object String]": "string-object",

        }
        if (target === null) {
            return 'null';
        }
        if (ret == 'object') {
            var str = Object.prototype.toString.call(target);
            return template[str];
        } else {
            return ret;
        }
    }

    // 数组去重
    var arr3 = [1, 1, 2, 2, 3, 3, 3, 3, 4, 8, 4, 8];

    Array.prototype.unique = function () {
        var temp = {},
            arr = [],
            len = this.length;
            for(var i = 0; i < len; i ++){
                if(!temp[this[i]]){
                    temp[this[i]] = 'abc';
                    arr.push(this[i]);
                }
            }
            return arr;
    }
    console.log(arr3.unique())
</script>